<!--
 * @Author: wuchao66_Git 2803670835@qq.com
 * @Date: 2023-12-09 10:36:13
 * @LastEditors: wuchao66_Git 2803670835@qq.com
 * @LastEditTime: 2023-12-23 11:24:55
 * @FilePath: \t167vuexg\src\views\login\index.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div class="boss">
    <div class="avater">
      <img src="../../assets/images/9e567d380cd79123c4d03e30a0345982b3b7809c.jpg">
    </div>
    <div class="container">
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
  <el-form-item label="账户" prop="pass">
    <el-input type="text" v-model="ruleForm.username" autocomplete="off"></el-input>
  </el-form-item>
  <el-form-item label="确认密码" prop="checkPass">
    <el-input type="text" v-model="ruleForm.password" autocomplete="off"></el-input>
  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
    <el-button @click="resetForm('ruleForm')">重置</el-button>
  </el-form-item>
</el-form>
    </div>



  </div>
</template>

<script>
import { mapMutations } from "vuex";
import {USERLOGIN} from '../../store/types'
import {login} from '../../api/user'
export default {
  data() {
      var checkAge = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('年龄不能为空'));
        }
        setTimeout(() => {
          if (!Number.isInteger(value)) {
            callback(new Error('请输入数字值'));
          } else {
            if (value < 18) {
              callback(new Error('必须年满18岁'));
            } else {
              callback();
            }
          }
        }, 1000);
      };
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkPass');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.ruleForm.pass) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return {
        ruleForm: {
          username: 'chaoge',
          password: '123456',
          age: ''
        },
        rules: {
          pass: [
            { validator: validatePass, trigger: 'blur' }
          ],
          checkPass: [
            { validator: validatePass2, trigger: 'blur' }
          ],
          age: [
            { validator: checkAge, trigger: 'blur' }
          ]
        }
      };
    },
    methods: {
      ...mapMutations([USERLOGIN]),//我的状态改变先数组中就一个数组
    //  async lll(){
    //  let res=   await this.login(this.ruleForm.username,this.ruleForm.password)
    //  }
     submitForm(formName) {
        this.$refs[formName].validate(async (valid) => {
          if (valid) {
            let result=await login(this.ruleForm.username,this.ruleForm.password);
            const token=  result.data.token;
            if (token != "") {
            // 保存token 用于权限验证
            this[USERLOGIN](token);
            this.$router.push("/main");
          } else {
            this.$message({
              message: "你的账号密码错误",
              type: "error",
            });
          }
        } else {
          console.log("失败!!");
          return false;
        }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  
}
</script>

<style  lang="less" scoped>
.avater,.container{
  display: flex;
  justify-content:center;//主轴居中
  align-items: center;//侧轴居中
  flex-direction: row;
}
.boss{
   background-color: rgb(1, 251, 251);
}
// .container{
//   width: 400px;
// }
  img{
    height: 200px;
    width: 200px;
    border-radius: 50%;
  }
</style>